package configlogic

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/recharge"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateRechargeTypeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateRechargeTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateRechargeTypeLogic {
	return &UpdateRechargeTypeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateRechargeTypeLogic) UpdateRechargeType(in *rpc.UpdateRechargeTypeRequest) (*rpc.UpdateRechargeTypeResponse, error) {

	tx := l.svcCtx.Db.Where("id = ?", in.Id).Updates(&recharge.RechargeType{
		SelectedIcon:   in.SelectedIcon,
		UnselectedIcon: in.UnselectedIcon,
		Name:           in.Name,
		InternalName:   in.InternalName,
		Status:         in.Status,
		Sort:           int32(in.Sort),
	})
	if tx.Error != nil || tx.RowsAffected == 0 {
		return nil, xerrors.ErrUpdate
	}
	return &rpc.UpdateRechargeTypeResponse{
		IsUpdate: true,
	}, nil
}
